﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Magicodes.ExcelImporter.Common
{
    public static class EnumHelper
    {
        public static string GetEnumDesc(Type e, int? value)
        {
            var fields = e.GetFields();
            for (int i = 1, count = fields.Length; i < count; i++)
            {
                if (fields[i].Name != "value__")
                {
                    if ((int) Enum.Parse(e, fields[i].Name) == value)
                    {
                        var enumAttributes = (DescriptionAttribute[]) fields[i].
                            GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (enumAttributes.Length > 0)
                            return enumAttributes[0].Description;
                    }
                }
            }
            return "";
        }

        public static Dictionary<string,int> GetEnumDescs(Type e)
        {
            var descriptionDictionarys = new Dictionary<string, int>();
            var fields = e.GetFields();
            for (int i = 1, count = fields.Length; i < count; i++)
            {
                var value = (int) Enum.Parse(e, fields[i].Name);
                var enumAttributes = (DescriptionAttribute[])fields[i].
                        GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (enumAttributes.Length > 0)
                    descriptionDictionarys.Add(enumAttributes[0].Description, value);
            }
            return descriptionDictionarys;
        }
    }
}